﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.Runtime.Serialization;

namespace WcfService
{
    [ServiceContract(Name = "WCFOverLoadingService")]
    public interface IWCFOverLoadingService
    {
        [OperationContract(Name = "SayHelloOverLoading1")]
        string SayHelloOverLoading();

        [OperationContract(Name = "SayHelloOverLoading2")]
        string SayHelloOverLoading(string name);

        [OperationContract(Name = "SayHelloOverLoading3")]
        string SayHelloOverLoading(string firstName, string lastName);
    }
    // 单调服务(Per-Call Service)
    // 为每次代理请求分配一个新的服务实例
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class WCFOverLoadingService : IWCFOverLoadingService, IDisposable
    {
        private int mCount = 0;

        public WCFOverLoadingService()
        {
            Console.WriteLine("WCFOverLoadingService[PerCall] Create mCount={0} ", mCount);
        }

        #region IWCFOverLoadingService 成员

        public string SayHelloOverLoading()
        {
            return SayHelloOverLoading("Default", string.Empty);
        }

        public string SayHelloOverLoading(string name)
        {
            return SayHelloOverLoading(name, string.Empty);
        }

        public string SayHelloOverLoading(string firstName, string lastName)
        {
            mCount++;
            Console.WriteLine("SayHello {0}-{1}", firstName, lastName);
            return firstName + " " + lastName;
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            Console.WriteLine("WCFOverLoadingService[PerCall] Disposable mCount={0} ", mCount);
        }

        #endregion
    }
}
